package cn.jathlon.io;

import java.util.Calendar;

/**
 * 日志类(所有都是打印至控制台)
 * @author chen
 *
 */
public class Log {

	private static long start;
	private static long nCalled = 0;
	
	/**
	 * 用来在程序中做标记
	 */
	public static void mark(String mark){
		System.out.println(String.format("[LOG.MARK]\t[%s]", mark));
	}
	
	public static void startTiming(){
		start = Calendar.getInstance().getTimeInMillis();
		System.out.println("[SYSTEM.TIMING]\t[Start...]");
	}
	
	/**
	 * 计算程序用时，返回毫秒数
	 * @return
	 */
	public static long calcRunningTime(){
		return Calendar.getInstance().getTimeInMillis() - start;
	}
	
	/**
	 * 计算程序用时，返回日志形式
	 * @return
	 */
	public static String finishTiming(){
		long diff = Calendar.getInstance().getTimeInMillis() - start;
		return String.format("[LOG.TIMING]\t[%s]", diff < 1000 ? diff + "ms" : diff/1000.0 + "s");
	}
	
	/**
	 * 打印一行
	 */
	public static void p(String content){
		System.out.println(String.format("[SYSTEM.OUT]\t%s", content));
	}
	
	/**
	 * 每调用该方法n行打印一次当前调用的次数
	 * @param n
	 */
	public static void pPerN(int n){
		if(++nCalled % n == 0){
			System.out.println(String.format("[LOG.INVOLKED]\t%s", nCalled));
		}
	}
	
	/**
	 * 每调用该方法n行打印一次指定内容
	 * @param n
	 */
	public static void pPerN(String content, int n){
		if(++nCalled % n == 0){
			System.out.println(String.format("[LOG.INVOLKED]\t%s", content));
		}
	}
	
	/**
	 * 打印接一个空格
	 */
	public static void pSpace(String content){
		System.out.print(content + " ");
	}
	
	public static void main(String[] args) {
		Log.startTiming();
		Log.mark("here");
		Log.p("hhaahah");
		for (int i = 0; i < 123; i++) {
			Log.p("hhaahah" + i*i*i);
		}
		Log.mark("OK...");
		
		///////////////////
		for (int i = 0; i < 100000; i++) {
			
			Log.pPerN(i + "", 10000);
		}
		System.out.println(Log.finishTiming());
	}
	
	
}

